﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.ZX.ViewModel;
using Com.ZX.View;

namespace Com.ZX.Manager
{
	public class ViewModelManager:BaseSingleton<ViewModelManager>
	{
       

        private Dictionary<string, BaseViewModel> dictViewModle = null;

		public ViewModelManager() : base()
		{ }

		public override void onInit()
		{
			base.onInit();
			dictViewModle = new Dictionary<string, BaseViewModel>();
		}
		/// <summary>
		/// 根据获取模块
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public BaseViewModel GetViewModel(string key)
		{
			if (dictViewModle.ContainsKey(key))
				return dictViewModle[key];
			return null;
		}

		/// <summary>
		/// 获取泛型Model
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public T GetViewModel<T>() where T :BaseViewModel,new()
		{
			Type t = typeof(T);
			if (dictViewModle.ContainsKey(t.ToString()))
				return (T)dictViewModle[t.ToString()];
			return null;
		}

        /// <summary>
        /// 获取Module
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        //public IView GetViewByModule<T>()
        //{
        //    Type t = typeof(T);
        //    //if(dictViewModle.ContainsKey(t.ToString()))
        //    //    return 
        //}

		/// <summary>
		/// 注册模块
		/// </summary>
		/// <param name="newModuleName"></param>
		public void RegistModel<T>(IView targetView) where T:BaseViewModel,new()
		{
            BaseViewModel newVM = new T();
            Type t = newVM.GetType();

            CustomDebug.Log("view:"+targetView.GetType().ToString());
            CustomDebug.Log("VM:" + newVM.GetType().ToString());

            RegistModel(t.ToString(),targetView, newVM);
        }

        /// <summary>
        /// 注册模块
        /// </summary>
        /// <param name="newModuleName"></param>
        /// <param name="newModule"></param>
        public void RegistModel(string newModuleName,IView uiView, BaseViewModel newModel)
        {
            if (dictViewModle != null && !dictViewModle.ContainsKey(newModuleName))
            {
                dictViewModle.Add(newModuleName, newModel);
                //TODO 关联UI Model
                uiView.BindContext = newModel;
            }
        }

		#region 反注册module
		
		/// <summary>
		/// 注销模块
		/// </summary>
		public void UnRegistModule(BaseViewModel module)
		{
			Type t = module.GetType();
			UnRegistModule(t.ToString());
		}
		/// <summary>
		/// 注销模块
		/// </summary>
		public void UnRegistModule(string key)
		{
			if (dictViewModle.ContainsKey(key))
			{
				BaseViewModel module = dictViewModle[key];
				module.Release();
				dictViewModle.Remove(key);
				module = null;
			}
		}
		
		/// <summary>
		/// 注销所有模块
		/// </summary>
		public void UnRegistModuleAll()
		{
			List<string> keys = new List<string>(dictViewModle.Keys);
			for (int i = 0; i < keys.Count; i++)
			{
				UnRegistModule(keys[i]);
			}
			dictViewModle.Clear();
			keys = null;
		}
        #endregion

       
    }
}
